﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Text;

using TempEmployees.BLL;
using TempEmployees.DAL;

namespace TempEmployees.Web
{
    public partial class LogView : ABasePage
    {
        private string WHERE
        {
            get { return ViewState["WHERE"] as string; }
            set { ViewState["WHERE"] = value; }
        }

        protected override void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Page.Form.DefaultButton = null;
                SetWhereClause();
                SetDateDropDown();
            }
            LogDataSource.Where = WHERE;
        }

        private void SetDateDropDown()
        {
            DateTime now = DateTime.Now;
            DateTime date;
            TimeSpan ts;
            // add today filter
            date = new DateTime(now.Year, now.Month, now.Day);
            DateDropDown.Items.Add(new ListItem("Today", date.ToString()));
            // 7 days
            ts = new TimeSpan(7, 0, 0, 0, 0);
            date = now - ts;
            DateDropDown.Items.Add(new ListItem("7 Days", date.ToString()));
            // 30 days
            ts = new TimeSpan(30, 0, 0, 0, 0);
            date = now - ts;
            DateDropDown.Items.Add(new ListItem("30 Days", date.ToString()));
            // all
            ts = new TimeSpan(90, 0, 0, 0, 0);
            date = now - ts;
            DateDropDown.Items.Add(new ListItem("90 Days", date.ToString()));
        }

        private void SetWhereClause()
        {
            StringBuilder where = new StringBuilder();

            //Severity
            if (SeverityDropDown.SelectedValue == "0") where.Append("Severity > 0");
            else where.Append(string.Format("Severity == {0}", SeverityDropDown.SelectedValue));

            //Date
            string date = (string.IsNullOrEmpty(DateDropDown.SelectedValue)) ? DateTime.Today.ToString()  : DateDropDown.SelectedValue;
            where.Append(string.Format(" && Date >= DateTime.Parse(\"{0}\")", date));

            //Message , Caller , Session ID , User ID
            where.Append(string.Format(" && Message.Contains(\"{0}\") && Caller.Contains(\"{1}\") && SessionID.Contains(\"{2}\") && UserID.Contains(\"{3}\")", 
                MessageTextBox.Text, CallerTextBox.Text, SessionIDTextBox.Text, UserIDTextBox.Text));

            WHERE = where.ToString();
            LogDataSource.Where = WHERE;
            LogGridView.DataBind();
        }

        protected void SetFilters_Event(object sender, EventArgs e) { SetWhereClause(); }

        protected void ClearButton_Click(object sender, EventArgs e)
        {
            SessionIDTextBox.Text = string.Empty;
            CallerTextBox.Text = string.Empty;
            MessageTextBox.Text = string.Empty;
            UserIDTextBox.Text = string.Empty;
            SeverityDropDown.SelectedValue = "0";
            DateDropDown.SelectedIndex = 0;
            SetWhereClause();
        }

        protected void LogDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            e.Result = (
                from entry in DbServices.db.LogEntries
                orderby entry.Date descending
                select entry).Take(int.Parse(ConfigurationManager.AppSettings["MAX_ENTRIES"]));
        }

        protected void ExportToExcel_Click(object sender, EventArgs e) { LogGridView.ExportToExcel(); }
    }
}